import type {
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  CrudExpose,
} from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { ActionEnum } from '#/enums/commonEnum';

import { ref } from 'vue';

import { useAccess } from '@vben/access';
import { $t } from '@vben/locales';

import { dict } from '@fast-crud/fast-crud';

import { BaseOrgApi } from '#/api/basic/user/baseOrg';
import { BasePositionApi } from '#/api/basic/user/basePosition';
import { PermCode } from '#/enums/perm';
import {
  createdTimeColumn,
  deleteButton,
  indexColumn,
  STATE_CONSTANT_DICT,
} from '#/plugins/fast-crud/common';

const { hasPermission } = useAccess();
const permCode = PermCode.basic.user.basePosition;

export function createCrudOptions(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  const selectedRowKeys = ref<string[]>([]);

  return {
    crudOptions: {
      request: {
        pageRequest: BasePositionApi.pageRequest,
        addRequest: BasePositionApi.addRequest,
        editRequest: BasePositionApi.editRequest,
        delRequest: BasePositionApi.delRequest,
        infoRequest: BasePositionApi.infoRequest,
      },
      actionbar: {
        buttons: {
          add: { show: hasPermission(permCode.add) },
          ...deleteButton({
            crudExpose: props.crudExpose,
            selectedRowKeys,
            removeFn: BasePositionApi.remove,
            role: permCode.delete,
          }),
        },
      },
      table: {
        striped: true,
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
          selectedRowKeys,
          onChange: (changed: string[]) => {
            selectedRowKeys.value = changed;
          },
        },
      },
      rowHandle: {
        width: '200px',
        buttons: {
          edit: { show: hasPermission(permCode.edit) },
          view: { show: true },
          remove: { show: hasPermission(permCode.delete) },
        },
      },
      columns: {
        ...indexColumn(props.crudExpose),
        name: {
          title: $t('basic.user.basePosition.name'),
          type: 'text',
          search: { show: true },
        },
        orgId: {
          title: $t('basic.user.basePosition.orgId'),
          type: 'dict-tree',
          column: { component: { type: 'text' } },
          dict: dict({
            isTree: true,
            value: 'id',
            label: 'name',
            async getData() {
              return await BaseOrgApi.tree({});
            },
          }),
        },
        state: {
          title: $t('basic.user.basePosition.state'),
          type: 'dict-radio',
          dict: STATE_CONSTANT_DICT,
          search: {
            show: true,
          },
          form: {
            component: {
              optionName: 'a-radio-button',
            },
            value: true,
          },
        },
        remarks: {
          title: $t('basic.user.basePosition.remarks'),
          type: 'textarea',
          search: { show: true },
        },
        ...createdTimeColumn({}),
      },
    },
  };
}

export const frontRules = (
  _crudExpose: CrudExpose,
  _mode: ActionEnum,
): FormRulesExt => {
  return {};
};
